<?php


class StringHelper
{
    public static function isValidString ($mix, $use_empty=False) {
        if (!is_string($mix)) {
            return false;
        }
        if (!$use_empty and $mix === "") {
            return false;
        }
        return true;
    }

    public static function isValidInt ($mix) {
        if (!is_int($mix)) {
            return false;
        }
        return true;

    }

    public static function isValidDatetimeString ($s) {
        $s = split(' ', trim($s));
        if (count($s) != 2) {
            return '输入错误';
        }
        $result = isValidDate($s[0]);
        if (!$result) {
            return false;
        }
        $result = isValidTime($s[1]);
        if (!$result) {
            return false;
        }
        return true;
    }

    public static function isValidDateString ($s) {
        if (preg_match('/^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$/', $s) != 0 ) {
            return true;
        } else {
            return false;
        }
    }

    public static function isValidTimeString ($s) {
        if( preg_match('/^(20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d([\.:]\d*)?$/', $s) != 0 ) {
            return true;
        } else {
            return false;
        }
    }


    function str2datestr($s) {
        $t = strtotime($s);
        if ($t === False) {
            return False;
        } else {
            return date('Y-m-d', $t);
        }
    }

    function utf8StringLen($s) {
        return mb_strlen($s, 'UTF-8');
    }

    function usubstr($s, $start=0, $count=-1) {
        return mb_substr($s, $start, $count, 'UTF-8');
    }



}


//////////// methods ///////////////////////////////////

function js_unescape($str) { 
    $str = rawurldecode($str); 
    preg_match_all("/%u.{4}|&#x.{4};|&#d+;|.+/U",$str,$r); 
    $ar = $r[0]; 
    foreach($ar as $k=>$v) { 
        if(substr($v,0,2) == "%u") 
            $ar[$k] = iconv("UCS-2","UTF-8",pack("H4",substr($v,-4))); 
        elseif(substr($v,0,3) == "&#x") 
            $ar[$k] = iconv("UCS-2","UTF-8",pack("H4",substr($v,3,-1))); 
        elseif(substr($v,0,2) == "&#") { 
            $ar[$k] = iconv("UCS-2","UTF-8",pack("n",substr($v,2,-1))); 
        } 
    } 
    return join("",$ar); 
} 

function str2datestr($s) {
    $t = strtotime($s);
    if ($t === False) {
        return False;
    } else {
        return date('Y-m-d', $t);
    }
}


function validate_str($s, $minlen=NULL, $maxlen=NULL) {
    $result = true;

    if (!is_string($s)) {
        return "必须为字符串";
    }
    $len = ustr_len(trim($s));
    if ($minlen !== NULL && $len == 0) {
        $result = "不能为空";
    } else if ($minlen !== NULL && $maxlen !== NULL) {
        if ($len > $maxlen)
            $result = "必须小于 $maxlen 个字";
    } else if ($minlen !== NULL && $maxlen === NULL) {
        if ($len < $minlen)
            $result = "必须大于 $minlen 个字";
    } else if ($minlen !== NULL && $maxlen !== NULL) {
        if ($len > $maxlen || $len < $minlen)
            $result = "必须在 $minlen 个字 到 $maxlen 个字以内";
    } else {
        $result = true;
    }


    return $result;
}

function validate_int($n, $min=NULL, $max=NULL) {
    $result = true;

    if(empty($n)){
        if($n!=0 || $n!="0"){
            return "必须为数字";
        }
    }
    if (is_string($n)) {
        $n = intval($n);
    }

    if (!is_int($n)) {
        return "必须为数字";
    }
    if ($min === NULL && $max !== NULL) {
        if ($n > $max)
            $result = "必须小于 $max ";
    } else if ($min !== NULL && $max === NULL) {
        if ($n < $min)
            $result = "必须大于 $min ";
    } else if ($min !== NULL && $max !== NULL) {
        if ($n > $max || $n < $min)
            $result = "必须在 $min 到 $max 以内";
    } else {
        $result = true;
    }

    return $result;
}


function validate_date($s) {
    if (preg_match('/^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$/', $s) != 0 ) {
        return true;
    } else {
        return "不是有效的日期格式";
    }
}

function validate_time($s) {
    if( preg_match('/^(20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d([\.:]\d*)?$/', $s) != 0 ) {
        return true;
    } else {
        return "不是有效的时间格式";
    }
}

function validate_datetime($s) {
    $s = split(' ', trim($s));
    if (count($s) != 2) {
        return '输入错误';
    }
    $result = validate_date($s[0]);
    if ($result !== true) {
        return $result;
    }
    $result = validate_time($s[1]);
    if ($result !== true) {
        return $result;
    }
    return true;
}




